package org.springblade.pm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springblade.pm.entity.Risk;
import org.springblade.pm.entity.RiskTemplate;
import org.springblade.pm.mapper.RiskMapper;
import org.springblade.pm.mapper.RiskTemplateMapper;
import org.springblade.pm.service.IRiskService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
@AllArgsConstructor
public class RiskServiceImpl extends ServiceImpl<RiskMapper,Risk> implements IRiskService {
	RiskMapper riskMapper;
	RiskTemplateMapper riskTemplateMapper;

	@Override
	public List<Object> getDesByCategory(Integer category) {
		QueryWrapper<RiskTemplate> wrapper = new QueryWrapper<>();
		wrapper.eq("category",category).select("des");
		return riskTemplateMapper.selectObjs(wrapper);
	}

	@Override
	public Integer saveRisk(Risk risk,Integer userId) {
		if (risk.getId() == null||"".equals(risk.getId().toString())) {
			risk.setCreateTime(new Date());
			risk.setUpdateTime(new Date());
			risk.setCreateUser(userId);
			risk.setUpdateUser(userId);
			return riskMapper.insert(risk);
		}else{
			risk.setUpdateTime(new Date());
			risk.setUpdateUser(userId);
			return riskMapper.updateById(risk);
		}
	}

	@Override
	public RiskTemplate getInsertDefaults(Integer category, String des) {
		QueryWrapper<RiskTemplate> wrapper = new QueryWrapper<>();
		wrapper.eq("category",category).eq("des",des);
		return riskTemplateMapper.selectOne(wrapper);

	}
}
